// pages/enrolldata/list.js
import ImgListCell from '../../template/ImgListCell';
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    keyword: "",
    searchPlaceholder: "",
    id:0,
    page: 1,
    dataList: [],
    // 显示加载更多 loading
    hothidden: true,
    loadinghidden: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    var id = options.id;
    if (id) {
      that.setData({
        id:id
      });
    }else{
      wx.showModal({
        title: '加载失败！',
        success: function (res) {
          if (res.confirm) {
            wx.navigateBack();
          }
        }
      })
      return;
    }

    Object.assign(this, ImgListCell);
    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    /**
     * 显示 loading
     */
    that.setData({
      loadinghidden: false
    });
    that.doLoadData();
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成
    // 页面渲染完成
    var that = this;

    // 数据加载完成后 延迟隐藏loading
    setTimeout(function () {
      that.setData({
        loadinghidden: true
      })
    }, 500);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.loadDetail();
  },
  searchContentInput: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  searchSubmit: function () {
    var that = this;

    that.doLoadData();
  },
  doLoadData: function () {
    var that = this;
    var parameter = {};
    parameter['enroll_id'] = that.data.id;
    parameter['keyword'] = that.data.keyword;
    parameter['page'] = 1;

    http.ApiGet(apiUrl.UrlEnrollDataList, parameter, function (res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.code == 1) {
        var newData = res.data;
        if (newData.length > 0) {

          util.removeNull(newData);

          newData = newData.map(function (item) {
            // item.title = JSON.stringify(item.data);

            return item;
          })
        } else {
          newData = [];
        }

        that.setData({
          page: 1,
          hothidden: false,
          dataList: newData
        });
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.doLoadData();
    this.loadDetail();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;

    // 加载更多 loading
    that.setData({
      hothidden: true
    })

    var page = that.data.page + 1;
    // 获取当前现有数据进行保存
    var list = that.data.dataList;
    var parameter = {};
    parameter['enroll_id'] = that.data.id;
    parameter['keyword'] = that.data.keyword;
    parameter['page'] = page;

    http.ApiGet(apiUrl.UrlEnrollDataList, parameter, function (res) {

      if (res.code == 1) {
        var newData = res.data;
        if (newData.length > 0) {

          util.removeNull(newData);

          that.setData({
            page: page,
            hothidden: false,
            dataList: list.concat(newData.map(function (item) {
              // item.title = JSON.stringify(item.data);

              return item;
            }))
          });
        } else {
          that.setData({
            hothidden: false
          })
        }
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  listCellTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = that.data.dataList[index];
    console.log(item.id);
    wx.navigateTo({
      url: '../enrolldata/detail?id=' + item.id
    })
  },
  loadDetail: function () {
    var that = this;
    var parameter = {};
    parameter['enroll_id'] = that.data.id;
    http.ApiGet(apiUrl.UrlEnrollDetail, parameter, function (res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.code == 1) {
        var detail = res.data;

        util.removeNull(detail);

        that.setData({
          loadinghidden: true,
          info: detail,
        });
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
})